from django.shortcuts import render
from django.views import generic

from . import forms
from cart.cart import Cart
from order.services.order import Order


class CreatingOrder(generic.View):

    """ Обработчик для создания заказа """

    def get(self, request):

        user_cart = Cart(request)
        form = forms.CreateOrderForm()
        context = {'form': form,
                   'user_cart': user_cart}

        return render(request, 'order/create.html', context)

    def post(self, request):

        bound_form = forms.CreateOrderForm(request.POST)

        if bound_form.is_valid():

            created_order = bound_form.save()
            user_order = Order(request)
            user_order.add(created_order)
            context = {'order': created_order}
            return render(request, 'order/created.html', context)
